package com.modelbuilder.controller;

import com.baidubce.qianfan.Qianfan;
import com.baidubce.qianfan.model.chat.ChatResponse;
import com.modelbuilder.dto.ChatRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/api/chat")
public class ChatController {

    @Autowired
    private Qianfan qianfan;

    @PostMapping
    public String chat(@RequestBody ChatRequest request) {
        try {
            ChatResponse response = qianfan.chatCompletion()
                    .model("ERNIE-Speed-128K")
                    .addMessage("user", request.getMessage())
                    .execute();
            return response.getResult();
        } catch (Exception e) {
            return "Error: " + e.getMessage();
        }
    }
}